package com.gz.controller;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gz.common.OrderService;
import com.gz.common.ProductService;
import com.gz.common.UserService;
import com.gz.common.model.Product;
import com.gz.common.model.User;
import com.gz.utils.Response;
import com.jfinal.core.Controller;

/**
 * 用户订单操作模块controller
 *
 * @author sfw
 */
public class OrderController extends Controller {

    /**
     * @return 存在返回1000 不存在返回1001
     * http://localhost:8080/cms-api/users/findopenId
     * @author sfw
     * Description:确认收货
     */
    public void confirmReceipt() {

        Integer orderid = getParaToInt("oid");
        Integer userid = getParaToInt("uid");
        User user = UserService.getService().findUserId(userid);    //查询用户信息a
        System.out.println("User:" + user.toString());
        //根据订单编号查询商品利润
        Product product = ProductService.getService().findProfit(orderid);
        Float profit = product.get("profit");    //商品利润
        profit = profit * 1000f;            //得到总利润
        System.out.println("product:" + profit);
        Integer integral = 0;            //z总积分
        Integer consumption = 0;        //消费积分
        Integer amountS = 0;            //提现积分
        int supersId = 0;                //上级编号
        User tempUser=null;
        //判断用户等级
        if (user.getUsergrade() == 1) {
            //1级用户
            integral = Math.round(profit * 0.5f);        //自身得到积分50%
            consumption = Math.round(integral * 0.1f);//自身得到消费积分10%
            amountS = Math.round(integral * 0.9f);        //自身得到提现积分90%
            UserService.getService().modifyIntegral(consumption, amountS, userid);
            //判断上级编号
            if (UserService.getService().findUserId(user.getSuperiorid()).getUsergrade()!= 3) {
                //给予上级添加积分  1
                integral = Math.round(profit * 0.2f);        //上级得到积分20%
                consumption = Math.round(integral * 0.1f);//上级得到消费积分10%
                amountS = Math.round(integral * 0.9f);        //上级得到提现积分90%
                UserService.getService().modifyIntegral(consumption, amountS, user.getSuperiorid());
                User superId1 = UserService.getService().findSuperId(user.getSuperiorid());
                supersId=superId1.getSuperiorid();
                if (UserService.getService().findUserId(supersId).getUsergrade()!= 3) {
                    //给予上级的上级添加积分
                    integral = Math.round(profit * 0.2f);        //经销商得到积分20%
                    consumption = Math.round(integral * 0.1f);//经销商得到消费积分10%
                    amountS = Math.round(integral * 0.9f);        //经销商得到提现积分90%
                    UserService.getService().modifyIntegral(consumption, amountS, supersId);
                    //再次判断此上级的上级编号是否是3
                    tempUser=UserService.getService().findUserId(UserService.getService().findSuperId(supersId).getSuperiorid());
                    if ( tempUser.getUsergrade()== 3) {
                        integral = Math.round(profit * 0.05f);        //总经销得到积分5%
                        consumption = Math.round(integral * 0.1f);//总经销得到消费积分10%
                        amountS = Math.round(integral * 0.9f);        //总经销得到提现积分90%
                        UserService.getService().modifyIntegral(consumption, amountS, UserService.getService().findSuperId(supersId).getSuperiorid());
                    }
                } else {
                    integral = Math.round(profit * 0.05f);        //总经销得到积分5%
                    consumption = Math.round(integral * 0.1f);//总经销得到消费积分10%
                    amountS = Math.round(integral * 0.9f);        //总经销得到提现积分90%
                    UserService.getService().modifyIntegral(consumption, amountS, supersId);
                }
            } else {
                integral = Math.round(profit * 0.05f);        //总经销得到积分5%
                consumption = Math.round(integral * 0.1f);//总经销得到消费积分10%
                amountS = Math.round(integral * 0.9f);        //总经销得到提现积分90%
                UserService.getService().modifyIntegral(consumption, amountS, user.getSuperiorid());
            }
        } else if (user.getUsergrade() == 2) {
            //2级用户
            integral = Math.round(profit * 0.6f);        //自身得到积分60%
            consumption = Math.round(integral * 0.1f);//自身得到消费积分10%
            amountS = Math.round(integral * 0.9f);        //自身得到提现积分90%
            UserService.getService().modifyIntegral(consumption, amountS, userid);
            //判断是否有上级
            if (UserService.getService().findUserId(user.getSuperiorid()).getUsergrade()!= 3) {
                //给予上级添加积分
                integral = Math.round(profit * 0.2f);        //上级得到积分20%
                consumption = Math.round(integral * 0.1f);//上级得到消费积分10%
                amountS = Math.round(integral * 0.9f);        //上级得到提现积分90%
                UserService.getService().modifyIntegral(consumption, amountS, user.getSuperiorid());
                User superId1 = UserService.getService().findSuperId(user.getSuperiorid());
                tempUser=UserService.getService().findUserId(UserService.getService().findSuperId(supersId).getSuperiorid());
                if (tempUser.getUsergrade()== 3) {
                    integral = Math.round(profit * 0.05f);        //总经销得到积分5%
                    consumption = Math.round(integral * 0.1f);//总经销得到消费积分10%
                    amountS = Math.round(integral * 0.9f);        //总经销得到提现积分90%
                    UserService.getService().modifyIntegral(consumption, amountS, superId1.getSuperiorid());
                }
            }else{
                integral = Math.round(profit * 0.05f);        //总经销得到积分5%
                consumption = Math.round(integral * 0.1f);//总经销得到消费积分10%
                amountS = Math.round(integral * 0.9f);        //总经销得到提现积分90%
                UserService.getService().modifyIntegral(consumption, amountS, user.getSuperiorid());
            }
        } else if (user.getUsergrade() == 3) {
            //3级用户
            integral = Math.round(profit * 0.7f);        //自身得到积分70%
            consumption = Math.round(integral * 0.1f);//自身得到消费积分10%
            amountS = Math.round(integral * 0.9f);        //自身得到提现积分90%
            UserService.getService().modifyIntegral(consumption, amountS, userid);

            integral = Math.round(profit * 0.2f);        //总经销的推荐人得到积分20%
            consumption = Math.round(integral * 0.1f);//总经销的推荐人得到消费积分10%
            amountS = Math.round(integral * 0.9f);        //总经销的推荐人得到提现积分90%
            UserService.getService().modifyIntegral(consumption, amountS, user.getSuperiorid());

        }
        //如果是购买升级专区的商品则升级用户
        UserService.getService().modifyUserGrade(userid, orderid);
        //查询用户上级编号
        User superId = UserService.getService().findSuperId(userid);
        //查询用户所属上级编号所有的下级分销商总数
        User countUser = UserService.getService().countSuperId(userid);
        System.out.println("superId:" + superId.getSuperiorid());
        System.out.println("countUser:" + countUser.get("count(1)"));
        Integer superiorid = superId.getSuperiorid();
        long count = countUser.get("count(1)");
        if (count >= 10 && superiorid != 0) {
            UserService.getService().modifySuper(superiorid);
        }
        if (OrderService.getService().confirmReceipt(orderid, userid) == 1) {

            renderJson(Response.responseJson(1001, "修改成功"));
        } else {
            renderJson(Response.responseJson(1000, "修改失败"));
        }

    }
}
